package oakclub.service;

import oakclub.background.BaseBackground;
import oakclub.global.Enums;
import oakclub.entity.BackgroundEntity;
import oakclub.global.Config;
import oakclub.global.Container;
import org.apache.commons.lang3.SerializationUtils;
import org.gearman.GearmanClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GearmanService
{
    private GearmanClient gearmanClient = (GearmanClient) Container.getComponent(Enums.Component.GEARMAN_CLIENT);
    private String prefix = Config.get("gearman.prefix");
    private final Logger logger = LoggerFactory.getLogger(GearmanService.class);

    public void submitBackground(Class<? extends BaseBackground> background, BackgroundEntity entity)
    {
        try
        {
            gearmanClient.submitBackgroundJob(this.prefix + background.getSimpleName(), SerializationUtils.serialize(entity));
        }
        catch(Exception e)
        {
            this.logger.error("submit background failed", e);
        }
    }
}
